﻿using System;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Web.UI;

namespace PQT
{
    public class PQTPagingDesigner : ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            PQTPager pager = (PQTPager)this.Component;
            RenderAtDesignTime(pager);

            return base.GetDesignTimeHtml();
        } // end of method GetDesignTimeHtml

        public void RenderAtDesignTime(PQTPager p_pager)
        {
            p_pager.Controls.Clear();

            string firstBtnCaption;
            string lastBtnCaption;
            string prevBtnCaption;
            string nextBtnCaption;

            // 처음 페이지
            if (p_pager.FirstPageImageUrl != string.Empty)
                firstBtnCaption = string.Format("<img src=\"{0}\" border=\"0\" align=\"absmiddle\">", p_pager.FirstPageImageUrl);
            else
                firstBtnCaption = p_pager.FirstPageText;

            // 마지막 페이지
            if (p_pager.LastPageImageUrl != string.Empty)
                lastBtnCaption = string.Format("<img src=\"{0}\" border=\"0\" align=\"absmiddle\">", p_pager.LastPageImageUrl);
            else
                lastBtnCaption = p_pager.LastPageText;

            // 이전 10페이지
            if (p_pager.PreviousButtonImageUrl != string.Empty)
                prevBtnCaption = string.Format("<img src=\"{0}\" border=\"0\" align=\"absmiddle\">", p_pager.PreviousButtonImageUrl);
            else
                prevBtnCaption = p_pager.PreviousButtonText;

            // 다음 10페이지
            if (p_pager.NextButtonImageUrl != string.Empty)
                nextBtnCaption = string.Format("<img src=\"{0}\" border=\"0\" align=\"absmiddle\">", p_pager.NextButtonImageUrl);
            else
                nextBtnCaption = p_pager.NextButtonText;

            // 숫자 페이징
            string[] buttons = new string[p_pager.PagerButtonCount + 2];

            for (int i = 1; i <= p_pager.PagerButtonCount; i++)
                buttons[i] = string.Format("[{0}]", i);

            buttons[0] = firstBtnCaption;
            buttons[1] = prevBtnCaption;
            buttons[buttons.Length - 2] = nextBtnCaption;
            buttons[buttons.Length - 1] = lastBtnCaption;

            string result = string.Join(" ", buttons, 0, buttons.Length);
            p_pager.Controls.Add(new LiteralControl(result));
        } // end of method RenderAtDesignTime
    }
}
